﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using BawangMemo.BackendModels;

namespace BawangMemo.BawangMemoView.View
{
    /// <summary>
    /// BankView.xaml 的交互逻辑
    /// </summary>
    public partial class BankView : UserControl
    {
        CourseWrapper _course;
        List<WordWrapper> _wordList;
        WordWrapper _word;        
        

        public BankView()
        {
            InitializeComponent();
        }

        public BankView(CourseWrapper wrapper)
        {
            _course = wrapper;            
            InitializeComponent();
            _wordList = _course.WordList;

            UpdateTagListBox();
            
            UpdateWordListBox();
            _word = _wordList[0];
            UpdateWordView();            
        }

        /// <summary>
        /// 为了生词本
        /// </summary>
        /// <param name="wrapper"></param>
        /// <param name="symbol"></param>
        public BankView(CourseWrapper wrapper, int symbol)
        {
            _course = wrapper;
            InitializeComponent();
            _wordList = _course.WordList;

            UpdateTagListBox();

            UpdateWordListBox();
            _word = _wordList[0];
            UpdateWordView();

            this.comboBox1.SelectedItem = "生词本";
        }

        #region Word&Tag Update

        void UpdateTagListBox()
        {
            List<string> list = TagRepository.ListAllTags(_course);
            comboBox1.ItemsSource = list;
        }

        void UpdateWordListBox()
        {
            listBox1.ItemsSource = _wordList;
            listBox1.SelectedIndex = -1;
        }

        void UpdateWordView()
        {
            if (_word != null)
            {
                wordView1.SetWord(_word);
                wordView1.ToggleExplanations(wordView1.Contents.Keys.ToArray<string>(), true);
            }
        }

        #endregion

        #region Word&Tag Event

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox1.SelectedIndex >= 0)
                _wordList = TagRepository.ListWordsOfTag(_course, comboBox1.SelectedItem.ToString());
            else
                _wordList = new List<WordWrapper>();
            UpdateWordListBox();
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox thistextbox = (TextBox)sender;            
            _wordList = _course.ListPrefixWords(thistextbox.Text);
            UpdateWordListBox();
            if (_wordList.Count != 0)
                _word = _wordList[0];
            UpdateWordView();
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBox1.SelectedIndex >= 0)
                _word = (WordWrapper)listBox1.SelectedItem;
            UpdateWordView();
        }

        private void wordView1_TagEventHandler(object sender, RoutedEventArgs e)
        {
            TagEventArgs args = (TagEventArgs)e;

            if (args.IsNew)
                _word.AddTag(args.Tag);
            else
                _word.RemoveTag(args.Tag);

            wordView1.Tags = TagRepository.ListWordTags(_word);
            UpdateTagListBox();
        }      
        #endregion              
    }
}
